package com.example.dmgame.fragment;

import java.util.ArrayList;
import java.util.List;

import com.example.dmgame.R;
import com.example.dmgame.activity.GameContentActivity;
import com.example.dmgame.adapter.GameAdapter;
import com.example.dmgame.bean.GameContent;
import com.example.dmgame.utils.JsonAsyncGameTask;
import com.example.dmgame.utils.JsonAsyncGameTask.Callback;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class GameFragment extends Fragment implements OnItemClickListener{
	Spinner spinner;
	GridView gameGridView;
	View view = null;
	int row = 10;
	int maxRow;
	String typeid = "190";
	FragmentManager manager;
	FragmentTransaction transaction;
	
	private List<String> list = new ArrayList<String>();
	private List<GameContent> gameContents = new ArrayList<GameContent>();
	GameAdapter adapter;
	private LinearLayout progressLayout;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		view = inflater.inflate(R.layout.game_fragment, container, false);
		
		//manager = getFragmentManager();
		spinner = (Spinner)view.findViewById(R.id.spinner1);
		list.add("游戏总分类");
		list.add("动作游戏(ACT)");
		list.add("赛车游戏(RAC)");
		list.add("体育游戏(SPG)");
		list.add("角色扮演(RPG)");
		list.add("射击游戏(FPS)");
		list.add("养成游戏(GAL)");
		list.add("益智游戏(PUZ)");
		list.add("即时战略(RTS)");
		list.add("策略游戏(SLG)");
		list.add("模拟经营(SIM)");
		list.add("冒险游戏(AVG)");
		list.add("动作角色(ARPG)");
		
		final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
		spinner.setAdapter(spinnerAdapter);
		
		
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				if(spinnerAdapter.getItem(position)=="游戏总分类"){
					typeid="179";
				}else if(spinnerAdapter.getItem(position)=="动作游戏(ACT)"){
					typeid="181";
				}else if(spinnerAdapter.getItem(position)=="赛车游戏(RAC)"){
					typeid="190";
				}else if(spinnerAdapter.getItem(position)=="体育游戏(SPG)"){
					typeid="188";
				}else if(spinnerAdapter.getItem(position)=="角色扮演(RPG)"){
					typeid="183";
				}else if(spinnerAdapter.getItem(position)=="射击游戏(FPS)"){
					typeid="182";
				}else if(spinnerAdapter.getItem(position)=="养成游戏(GAL)"){
					typeid="184";
				}else if(spinnerAdapter.getItem(position)=="益智游戏(PUZ)"){
					typeid="185";
				}else if(spinnerAdapter.getItem(position)=="即时战略(RTS)"){
					typeid="186";
				}else if(spinnerAdapter.getItem(position)=="策略游戏(SLG)"){
					typeid="187";
				}else if(spinnerAdapter.getItem(position)=="模拟经营(SIM)"){
					typeid="189";
				}else if(spinnerAdapter.getItem(position)=="冒险游戏(AVG)"){
					typeid="191";
				}else if(spinnerAdapter.getItem(position)=="动作角色(ARPG)"){
					typeid="192";
				}
				loadGameContent();
			}
			
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				typeid = "190";
			}
		});
		
		
		Log.i("GameFragment", "----onCreateView--->>gameContents"+gameContents);
		
		gameGridView = (GridView)view.findViewById(R.id.game_gridview);
		progressLayout = (LinearLayout)view.findViewById(R.id.progressLayoutId);
		
		adapter = new GameAdapter(gameContents, getActivity(), gameGridView);
		
		gameGridView.setAdapter(adapter);
		gameGridView.setOnScrollListener(new OnScrollListener() {
			private boolean isBottom = false;
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				switch (scrollState) {
				case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					break;
				case OnScrollListener.SCROLL_STATE_FLING:
					break;
				case OnScrollListener.SCROLL_STATE_IDLE:
					if(isBottom){
						Log.i("News_listviewFragment", "---->>row:"+row+",maxRow:"+maxRow);
						if(row<=maxRow){
							loadGameContent();
						}
					}
				}
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				isBottom = (firstVisibleItem+visibleItemCount)==totalItemCount;
			}
		});
		gameGridView.setOnItemClickListener(this);
		loadGameContent();
		return view;
	}
	

	
	private void loadGameContent(){
		JsonAsyncGameTask task = new JsonAsyncGameTask(new Callback() {
			
			@Override
			public void sendData(List<GameContent> gameContents, int row) {
				// TODO Auto-generated method stub
				Log.i("GameFragment", "----loadGameContent--->>gameContents"+gameContents);
				//gameContents.addAll(gameContents);
				maxRow = row;
				GameFragment.this.gameContents.clear();
				//GameFragment.this.gameContents = gameContents;
				GameFragment.this.gameContents.addAll(gameContents);
				adapter.notifyDataSetChanged();
				progressLayout.setVisibility(View.GONE);
			}
		}, row);
		
		task.execute(loadGameContemtURL(row+=9, typeid));
		progressLayout.setVisibility(View.VISIBLE);
		
	}
	
	private String loadGameContemtURL(int row,String typeid){
		return "http://www.3dmgame.com/sitemap/api.php?row="+row+"&typeid="+typeid+"&paging=1&page=n";
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Log.i("GameFragment", "----onItemClick-->>:"+gameContents);
		GameContent gameContent = gameContents.get(position);
		
		
		
		Bundle bundle = new Bundle();
		bundle.putString("litpic", gameContent.getLitpic());
		bundle.putString("title", gameContent.getTitle());
		bundle.putString("pubdate", gameContent.getPubdate());
		bundle.putString("typename", gameContent.getTypename());
		bundle.putString("made_company", gameContent.getMade_company());
		bundle.putString("release_company", gameContent.getRelease_company());
		bundle.putString("website", gameContent.getWebsite());
		bundle.putString("description", gameContent.getDescription());
		bundle.putString("terrace", gameContent.getTerrace());

		Intent intent = new Intent(getActivity(), GameContentActivity.class);
		intent.putExtra("gamecontent", bundle);
		startActivity(intent);
		
	}
  
}
